﻿//========================================================= 
/**@file LmdbEnumerator.h
 * @brief lmdb枚举器
 * 
 * @date 2018-06-07   10:26:31
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_LMDBENUMERATOR_H_
#define _LIBZHOUYB_LMDBENUMERATOR_H_
//--------------------------------------------------------- 
#include "lmdb_extractor.h"
//--------------------------------------------------------- 
namespace zhou_yb {
namespace wrapper {
namespace lmdb_extractor {
//--------------------------------------------------------- 
/**
 * @brief lmdb KV枚举器
 * @date 2018-06-05 15:27
 */
class LmdbEnumerator : public IEnumerable<ArgValue<ByteArray, ByteArray> >
{
public:
    typedef ArgValue<ByteArray, ByteArray> ArgType;
protected:
    lmdb_const_iterator _itr;
    ArgType _current;
    bool _isEof;
public:
    LmdbEnumerator(lmdb_const_dbi& dbi) : _itr(dbi.cursor()) { Reset(); }
    LmdbEnumerator(lmdb_dbi& dbi)
    {
        lmdb_const_dbi readonly_dbi = dbi;
        _itr = readonly_dbi.cursor();
        Reset();
    }
    LmdbEnumerator(const lmdb_cursor& cursor) : _itr(cursor) { Reset(); }
    LmdbEnumerator(const lmdb_const_cursor& cursor) : _itr(cursor) { Reset(); }

    /// 迭代接口
    virtual bool MoveNext()
    {
        if(_isEof)
            return false;

        _current.Key = _itr.Key;
        _current.Value = _itr.Value;

        _isEof = !_itr.next();
        return true;
    }
    /// 重置迭代状态
    virtual void Reset()
    {
        _isEof = !_itr.first();
    }
    /// 获取当前的值
    virtual ArgType& Current()
    {
        return _current;
    }
    /// 获取当前的值(只读)
    virtual const ArgType& Current() const
    {
        return _current;
    }
    /// 枚举是否结束
    virtual bool IsEof() const
    {
        return _isEof;
    }
};
/**
 * @brief lmdb 字符串KV枚举器
 * @date 2018-06-07 10:07
 */
class LmdbStringEnumerator : public IEnumerable<ArgValue<string, string> >
{
public:
    typedef ArgValue<string, string> ArgType;
protected:
    lmdb_string_const_iterator _itr;
    ArgType _current;
    bool _isEof;
public:
    LmdbStringEnumerator() : _isEof(true) {}
    LmdbStringEnumerator(lmdb_const_dbi& dbi) : _itr(dbi.cursor()) { Reset(); }
    LmdbStringEnumerator(lmdb_dbi& dbi)
    {
        lmdb_const_dbi readonly_dbi = dbi;
        _itr = readonly_dbi.cursor();
        Reset();
    }
    LmdbStringEnumerator(const lmdb_cursor& cursor) : _itr(cursor) { Reset(); }
    LmdbStringEnumerator(const lmdb_const_cursor& cursor) : _itr(cursor) { Reset(); }

    /// 迭代接口
    virtual bool MoveNext()
    {
        if(_isEof)
            return false;

        _current.Key = _itr.Key;
        _current.Value = _itr.Value;

        _isEof = !_itr.next();
        return true;
    }
    /// 重置迭代状态
    virtual void Reset()
    {
        _isEof = !_itr.first();
    }
    /// 获取当前的值
    virtual ArgType& Current()
    {
        return _current;
    }
    /// 获取当前的值(只读)
    virtual const ArgType& Current() const
    {
        return _current;
    }
    /// 枚举是否结束
    virtual bool IsEof() const
    {
        return _isEof;
    }
};
//--------------------------------------------------------- 
} // namespace lmdb_extractor
} // namespace wrapper
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_LMDBENUMERATOR_H_
//========================================================= 